package question;

/**
 * 问题2：送礼品
 *
 * @author 阳秋豪
 * @描述 有100份礼品, 两人同时发送，当剩下的礼品小于10份的时候则不再送出利用多线程模拟该过程并将线程的名字和礼物的剩余数量打印出来
 * @createTime 2024/3/12
 */
public class Test2 {
    public static void main(String[] args) {
        QOneRunnable2 runnable = new QOneRunnable2();
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);
        t1.setName("送礼人A");
        t2.setName("送礼人B");
        t1.start();
        t2.start();
    }
}

class QOneRunnable2 implements Runnable {
    int gift = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (QOneRunnable2.class) {
                if (gift < 10) {
                    break;
                } else {
                    gift--;
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName() + "送出了一份礼物，还剩" + gift + "份礼物");
                }
            }
        }
    }
}